package br.ufrj.cos.bri;

import java.io.File;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import br.ufrj.cos.bri.preprocessing.TextCleaner;


public class DataLoad {
	
	public DataBaseComunicator dbCom;
	public TextCleaner textCleaner;
	
	public DataLoad(){
		dbCom = new DataBaseComunicator();
		dbCom.createConnection();
		textCleaner = new TextCleaner();
	}
	
	public void loadDocs(NodeList docList){
		
		
		for(int i=0; i<docList.getLength(); i++){
			
			
			
			Node docElement = docList.item(i);
			NodeList docSubElements = docElement.getChildNodes();
			
			String recordNum = "";
			String title = "";
			String abst = "";
			String corpo = "";
			
			for(int j=0; j<docSubElements.getLength(); j++){
				Node docSubElement = docSubElements.item(j);
				if(docSubElement.getNodeName().equalsIgnoreCase("recordnum")){
					recordNum = docSubElement.getTextContent().trim();
					recordNum = Integer.parseInt(recordNum) + "";
				}
				if(docSubElement.getNodeName().equalsIgnoreCase("title")){
					if(recordNum == ""){
					}
					title = docSubElement.getTextContent();
					//mecanismo de preprocessamento do title
					title = textCleaner.clean(title);
					
					
				}
				if(docSubElement.getNodeName().equalsIgnoreCase("abstract")){
					abst = docSubElement.getTextContent();
					//mecanismo de preprocessamento do abstract
					abst = textCleaner.clean(abst);
				}
			}
			corpo = getTextFromDocument(docElement);
			//mecanismo de preprocessamento do abstract
//			corpo = textCleaner.clean(corpo);

				
	
			insertDoc(recordNum, title, abst, corpo);
			if(abst!= ""){
					InvertedListManager.getInstance().addDocumentTerms(recordNum, abst);
			}
//			InvertedListManager.getInstance().addDocumentTerms(recordNum, title);
		}
		
	}
		
	public String getTextFromDocument(Node element){
		String text = "";
		
		NodeList children = element.getChildNodes();
		
		if(element.getNodeName().equalsIgnoreCase("CITE")){
			String author = ((Element)element).getAttribute("author");
			if(author != null)
				text += " "+author.trim();
			
			String publication = ((Element)element).getAttribute("publication");
			if(publication != null)
				text += " "+publication.trim();
		}
		
		String value = element.getNodeValue();
		if(value != null)
			text += " " + value.trim();
		
		for(int i=0; i<children.getLength(); i++){
			Node child = children.item(i);
			text += " "+getTextFromDocument(child);
		}
		
		return text;
	}

	public void loadQueries(NodeList queryList){
		
		
		for(int i=0; i<queryList.getLength(); i++){
			Node queryElement = queryList.item(i);
			
			String queryNum = null;
			String queryText = null;
			
			queryNum = queryElement.getChildNodes().item(0).getTextContent().trim();
			queryNum = Integer.parseInt(queryNum)+"";
			queryText = queryElement.getChildNodes().item(1).getTextContent();
			
			//mecanismo de preprocessamento para consulta
			queryText = textCleaner.clean(queryText);
			
			
			insertQuery(queryNum, queryText);
			
			NodeList listRecordsResults = queryElement.getChildNodes().item(3).getChildNodes();
			
			for(int j=0; j<listRecordsResults.getLength(); j++){
				Node itemRecord = listRecordsResults.item(j);
				String itemScore = itemRecord.getAttributes().getNamedItem("score").getTextContent();
				
				String relevance1 = itemScore.substring(0, 1);
				String relevance2 = itemScore.substring(1, 2);
				String relevance3 = itemScore.substring(2, 3);
				String relevance4 = itemScore.substring(3, 4);
				
				String docRecordNum = itemRecord.getTextContent().trim();
				
				docRecordNum = Integer.parseInt(docRecordNum)+"";
				
//				String temp = "00000"; 
//				int recordNumLength = docRecordNum.length();
//				
//				docRecordNum = temp.substring(0, 5-recordNumLength) + docRecordNum; 
				
				insertResult(queryNum, docRecordNum, relevance1, relevance2, relevance3, relevance4);

			}
			
		}
		
		
	}
	



	public void insertDoc(String recordNum, String title, String abst, String corpo){
		dbCom.insertDoc(recordNum, title, abst, corpo);
	}
	
	
	public void insertQuery(String queryNum, String queryText){
		dbCom.insertQuery(queryNum, queryText);
	}
	
	public void insertResult(String queryNum, String docRecordNumber, String relevance1, String relevance2, String relevance3, String relevance4){
		dbCom.insertResult(queryNum, docRecordNumber, relevance1, relevance2, relevance3, relevance4);
	}
	
	
	public void createInvertedListManager(String field) throws SQLException{
		ResultSet result;
		
		//todos os docs
		result = dbCom.executeQuery("SELECT * FROM doc");
		//result.next();
		
		while(result.next()){
			int recordNum = result.getInt(1);
			String fieldText = result.getString(field);
			InvertedListManager.getInstance().addDocumentTerms(String.valueOf(recordNum), fieldText);
		}
		
		InvertedListManager.getInstance().calculateTF_IDF_Docs(field);
		InvertedListManager.getInstance().saveInvertedList(new File("docs/data.ser"));
	}
}